package com.bootdo.wx.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.domain.MemberAddressDO;
import com.bootdo.wx.domain.MemberExtractDO;
import com.bootdo.wx.domain.MemberSignRewardDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberAddressService;
import com.bootdo.wx.service.MemberSignRewardService;
import com.bootdo.wx.service.MemberUserService;
/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-07-16 10:40:15
 */
 
@Controller
@RequestMapping("/wx/memberSignReward")
public class MemberSignRewardController {
	@Autowired
	private MemberSignRewardService memberSignRewardService;
	@Autowired
	private MemberUserService memberUserService;
	
	@Autowired
	private MemberAddressService memberAddressService;
	
	@GetMapping()
	@RequiresPermissions("wx:memberSignReward:memberSignReward")
	String MemberSignReward(){
	    return "wx/memberSignReward/memberSignReward";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberSignReward:memberSignReward")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<MemberSignRewardDO> memberSignRewardList = memberSignRewardService.likeList(query);
            List<Map<String, Object>> list = new ArrayList<>(); 
            if(memberSignRewardList.size()>0){
            	for (MemberSignRewardDO memberSignRewardDO : memberSignRewardList) {
            		Map<String, Object> entityToMap = StringUtils.entityToMap(memberSignRewardDO);
            		MemberUserDO memberUserDO = memberUserService.get(memberSignRewardDO.getUserId());
            		if(memberUserDO!=null){
            			entityToMap.put("userName", memberUserDO.getName());
            			entityToMap.put("userPhone", memberUserDO.getPhone());
            		}else{
            			entityToMap.put("userName", "");
            			entityToMap.put("userPhone", "");
            		}
            		list.add(entityToMap);
				}
            }
            int total = memberSignRewardList.size();
            PageUtils pageUtils = new PageUtils(list, total);
            return pageUtils;
		}

		List<MemberSignRewardDO> memberSignRewardList = memberSignRewardService.list(query);
		List<Map<String, Object>> list = new ArrayList<>(); 
		if(memberSignRewardList.size()>0){
         	for (MemberSignRewardDO memberSignRewardDO : memberSignRewardList) {
         		Map<String, Object> entityToMap = StringUtils.entityToMap(memberSignRewardDO);
        		MemberUserDO memberUserDO = memberUserService.get(memberSignRewardDO.getUserId());
        		if(memberUserDO!=null){
        			entityToMap.put("userName", memberUserDO.getName());
        			entityToMap.put("userPhone", memberUserDO.getPhone());
        		}else{
        			entityToMap.put("userName", "");
        			entityToMap.put("userPhone", "");
        		}
        		list.add(entityToMap);
			}
         }
		int total = memberSignRewardService.count(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberSignReward:add")
	String add(){
	    return "wx/memberSignReward/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberSignReward:edit")
	String edit(@PathVariable("id") String id,Model model){
		MemberSignRewardDO memberSignReward = memberSignRewardService.get(id);
		model.addAttribute("memberSignReward", memberSignReward);
	    return "wx/memberSignReward/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberSignReward:add")
	public R save( MemberSignRewardDO memberSignReward){
	    memberSignReward.setId(UuidUtil.get32UUID());
	    memberSignReward.setDelFlag(0);
	    memberSignReward.setCaAt(new Date());
		if(memberSignRewardService.save(memberSignReward)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberSignReward:edit")
	public R update( MemberSignRewardDO memberSignReward){
		memberSignReward.setUpAt(new Date());
		memberSignRewardService.update(memberSignReward);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberSignReward:remove")
	public R remove( String id){
		MemberSignRewardDO memberSignReward = memberSignRewardService.get(id);
		memberSignReward.setDelFlag(1);
		memberSignReward.setUpAt(new Date());
		if(memberSignRewardService.update(memberSignReward)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberSignReward:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MemberSignRewardDO memberSignReward = memberSignRewardService.get(string);
				memberSignReward.setDelFlag(1);
				memberSignReward.setUpAt(new Date());
				memberSignRewardService.update(memberSignReward);
			}
		}
	
		return R.ok();
	}
	
	//获取收货地址
	@GetMapping("/getAddress/{id}")
	@RequiresPermissions("wx:memberSignReward:edit")
	String getAddress(@PathVariable("id") String id,Model model){
		MemberSignRewardDO memberSignReward = memberSignRewardService.get(id);
		MemberAddressDO memberAddressDO = memberAddressService.get(memberSignReward.getAddressId());
		model.addAttribute("memberAddressDO", memberAddressDO);
	    return "wx/memberSignReward/getAddress";
	}
	
	/**
	 * 审核
	 */
	@ResponseBody
	@PostMapping("/updateStatus")
	@RequiresPermissions("wx:memberSignReward:edit")
	public R shen(MemberSignRewardDO memberSignReward){
		memberSignReward.setUpAt(new Date());
		int update = memberSignRewardService.update(memberSignReward);
		return R.ok();
	}
	
}
